﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.Departmentss
{
    public class ShowDepartmentHander : IRequestHandler<ShowDepartmentCommand, Result<PageResultDto<DepartmentDto>>>
    {
        /// <summary>
        /// 部门表
        /// </summary>
        private readonly IBaseRepository<Departments> _departmentsRepository;

        /// <summary>
        /// 部门类型
        /// </summary>
        private readonly IBaseRepository<DepartmentType> _departmentTypeRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="departmentsRepository"></param>
        /// <param name="departmentTypeRepository"></param>
        public ShowDepartmentHander(IBaseRepository<Departments> departmentsRepository,
            IBaseRepository<DepartmentType> departmentTypeRepository)
        {
            _departmentsRepository = departmentsRepository;
            _departmentTypeRepository = departmentTypeRepository;
        }
        /// <summary>
        /// 部门显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<PageResultDto<DepartmentDto>>> Handle(ShowDepartmentCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<DepartmentDto>>() { Code = (int)CommonErrorCode.Search };
            try
            {
                var page = new PageResultDto<DepartmentDto>() { List = new List<DepartmentDto>(), PageCount = 0, TotalCount = 0 };
                //获取部门信息
                var query = (from a in await _departmentsRepository.GetAll()
                             join b in await _departmentTypeRepository.GetAll() on a.DepartmentTypeId equals b.DepartmentTypeId
                             select new DepartmentDto()
                             {
                                 DepartmentId = a.DepartmentId,
                                 Pid = a.Pid,
                                 DepartmentName = a.DepartmentName,
                                 DepartmentStatus = a.DepartmentStatus,
                                 DepartmentTypeId = a.DepartmentTypeId,
                                 DepartmentAreaIds = a.DepartmentAreaIds,
                                 DepartmentAreaNames = a.DepartmentAreaNames,
                                 DirectSupervisor = a.DirectSupervisor,
                                 DepartmentManager = a.DepartmentManager,
                                 ResponsiblePerson = a.ResponsiblePerson,
                                 FinanceSupervisor = a.FinanceSupervisor,
                                 Viewableperson = a.Viewableperson,
                                 Functionalsystem = a.Functionalsystem,
                                 Operator = a.Operator,
                                 OperationTime = a.OperationTime,
                                 DepartmentTypeName = b.DepartmentTypeName,
                             });
                //部门名称模糊查
                if (!string.IsNullOrEmpty(request.DepartmentName))
                {
                    query = query.Where(x => x.DepartmentName.Contains(request.DepartmentName));
                }
                //部门类型精确查
                if (request.DepartmentTypeId != 0)
                {
                    query = query.Where(x => x.DepartmentTypeId == request.DepartmentTypeId);
                }
                //部门状态精确查
                if (request.DepartmentStatus != 0)
                {
                    query = query.Where(x => x.DepartmentStatus == request.DepartmentStatus);
                }
                //总条数
                page.TotalCount = query.Count();
                //总页数
                page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / request.PageSize);
                //跳过几条  显示几条
                page.List = query.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                if (query != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = page;
                    result.Msg = "获取部门信息成功";
                }
            }
            catch (Exception ex)
            {
                result.Msg = "获取部门信息异常" + ex.Message;
            }

            return result;
        }
    }
}
